<?php

/**
 * AfroSoft ACL API
 *
 * @package Exception
 *
 * @author AfroSoft
 * @version @build.version@ (Build @build.number@)
 * @license http://www.mozilla.org/MPL/MPL-1.1.txt MPL 1.1
 * @copyright Copyright (c) AfroSoft
 */
/* * **** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the AfroSoft ACL API.
 *
 * The Initial Developer of the Original Code is AfroSoft.
 * Portions created by the Initial Developer are Copyright (C) 2011 the Initial
 * Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Xavier L. <xavier.l@afrosoft.tk>
 *
 * ***** END LICENSE BLOCK ***** */

/**
 * This is a generic ACL Exception.
 */
class ACLException extends Exception {

}

/**
 * This exception is thrown when a user tries define an item that has already
 * been defined.
 */
class ItemAlreadyDefinedException extends ACLException {

}

/**
 * This exception is thrown when a user tries to set a permission on a
 * non-existing item.
 */
class ItemNotDefinedException extends ACLException {

}

/**
 * This exception is thrown when a user tries to set a permission on an item
 * that has already been set.
 */
class PermissionAlreadySetException extends ACLException {

}

/**
 * This exception is thrown when the user tries to accomplish something with
 * insufficient priviledges.
 */
class PermissionNotSetException extends ACLException {

}

/**
 * This exception is thrown when the database object (ACLRequest::$dbi or
 * ACLAdmin::$dbi) is not specified.
 */
class ACLDatabaseNotConnectedException extends ACLException {

}

/**
 * This exception is thrown when a required parameter is not passed to a
 * function.
 */
class MissingRequiredArgumentException extends InvalidArgumentException {
    /**
     * @param array $keys The required parameters
     * @param int $code
     * @param Exception $previous Silently dropped, as not implemented prior to
     * PHP 5.3.0
     */
    public function __construct($keys, $code = 0, $previous = NULL) {
        $message = 'All the required arguments (' . implode(', ', $keys) . ') must be set.';
        parent::__construct($message, $code);
    }

}

/**
 * This exception is thrown when no required parameter from a list is passed to
 * a function.
 */
class MissingListRequiredArgumentException extends InvalidArgumentException {
    /**
     * @param array $keys The required parameters
     * @param int $code
     * @param Exception $previous Silently dropped, as not implemented prior to
     * PHP 5.3.0
     */
    public function __construct($keys, $code = 0, $previous = NULL) {
        $message = 'At least one of the following required arguments (' . implode(', ', $keys) . ') must be set.';
        parent::__construct($message, $code);
    }

}

/**
 * This exception is thrown when the type parameter is set to an unrecognized 
 * value.
 */
class InvalidTypeArgumentException extends InvalidArgumentException {
    /**
     * @param mixed $message The required parameters
     * @param int $code
     * @param Exception $previous Silently dropped, as not implemented prior to
     * PHP 5.3.0
     */
    public function __construct($message = null, $code = 0, $previous = NULL) {
        parent::__construct('The \'type\' parameter must be either \'user\' or \'group\'.', $code);
    }
    
}

/**
 * This exception is thrown when the user trying to accomplish an action holds
 * insufficient permissions. It can be reused in your code.
 */
class InsufficientPermissionsException extends ACLException {

    /**
     * The key of the item
     */
    public $key;
    /**
     * The user trying to accomplish the action
     */
    public $user;

    /**
     * @param string $key The key of the item
     * @param mixed $user The user trying to accomplish the action
     * @param int $code
     * @param Exception $previous Silently dropped, as not implemented prior to
     * PHP 5.3.0
     */
    public function __construct($key, $user, $code = 0, $previous = NULL) {
        $this->key = $key;
        $this->user = $user;
        $message = 'User \'' . $user . '\' has insufficient permission on item \'' . $key . '\'.';
        parent::__construct($message, $code);
    }

}
